#include "buzzer.h"


const unsigned int beep_on = 432;
const unsigned int beep_off = 666;

static void buzzer_callback_default(){}
	
static function * buzzer_start = buzzer_callback_default;
static function * buzzer_stop  = buzzer_callback_default;

static unsigned int beep_count = 0;
static unsigned int beep_repeat = 0;


void buzzer_callback()
{
	if(beep_repeat > 0)
	{
		beep_count++;
		if(beep_count < beep_on)
		{
			BUZZER_CTRL = !BUZZER_CTRL;
		}
		else if(beep_count < beep_off)
		{
			BUZZER_CTRL = 0;
		}
		else
		{
			beep_count = 0;
			beep_repeat--;
		}
	}
	else
	{
		buzzer_stop();
	}
}

void buzzer_init(function * fpstart, function * fpstop)
{
	buzzer_start		= fpstart;
	buzzer_stop			= fpstop;
}

void buzzer_beep()
{
	buzzer_stop();
	beep_repeat++;
	buzzer_start();
}


void buzzer_beepex(unsigned char n)
{
	buzzer_stop();
	beep_repeat += n;
	buzzer_start();
}

void buzzer_beep_synchronous(unsigned char n)
{
	buzzer_stop();
	beep_repeat = n;
	buzzer_start();
}
